package ute.mvc.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ute.mvc.dao.ClassDAO;
import ute.mvc.dao.UserDAO;
import ute.mvc.model.MyClass;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.TestAndQuizz;
import ute.mvc.model.User;

@Service("classService")
public class ClassServiceImpl implements ClassService {

	@Autowired
	ClassDAO classDAO;
	@Autowired
	UserDAO userDAO;

	public List<MyClass> getAllClass() {
		return classDAO.getAllClass();
	}

	public List<MyClass> findClassByName(String className) {
		return classDAO.findClassByName(className);
	}

	@Transactional
	public void addClass(MyClass myClass) {
		classDAO.addClass(myClass);
	}

	@Transactional
	public void updateClass(MyClass myClass) {
		classDAO.updateClass(myClass);
	}

	@Transactional
	public void deleteClass(MyClass myClass) {
		classDAO.deleteClass(myClass);
	}

	public MyClass getClassById(String classId) {
		return classDAO.getClassById(classId);
	}

	public List<User> getListStudentByClassId(String classId) {
		return classDAO.getListUserByClassId(classId);
	}

	public MyClass getClassByIdWithListStudent(String classId) {
		return classDAO.getClassByIdWithListStudent(classId);
	}

	public List<User> getAddUserById(String classId) {
		boolean f;
		
		List<User> listStudent = userDAO.getAllStudent();
		MyClass myClass = classDAO.getClassById(classId);
		List<MyClass> listClass = new ArrayList<MyClass>(myClass.getOpenCourse().getListClass());
		List<User> listResult = new ArrayList<User>();
		
		
		for(User user : listStudent) {
			f = false;
			for(MyClass c : listClass) {
				List<User> userInClass = new ArrayList<User>(c.getListStudent());
				for(User u : userInClass) {
					if(user.equals(u)) {
						f = true;
						break;
					}
				}
				if(f == true)
					continue;
			}
			
			if(f == false) {
				listResult.add(user);
			}
		}
		
		
		return listResult;
	}


	// Vinh
	public List<TestAndQuizz> getAllTestInOpenCourseSubject(
			OpenCourse_Subject courseSubject, String classId) {
		// TODO Auto-generated method stub
		List<TestAndQuizz> lstTest = new ArrayList<TestAndQuizz>();
		lstTest.addAll(courseSubject.getListTestAndQuizz());
		List<TestAndQuizz> list = new ArrayList<TestAndQuizz>();
		for (TestAndQuizz test : lstTest) {
			List<MyClass> lstClass = new ArrayList<MyClass>();
			lstClass.addAll(test.getListClass());
			for (MyClass c : lstClass) {
				if (c.getClassId().equals(classId)) {
					list.add(test);
					break;
				}
			}
		}
		return list;
	}

	public boolean removeStudent(String[] emails, String classId) {

		MyClass myClass = classDAO.getClassById(classId);
		try {
			for (String email : emails) {
				Collection<User> list = myClass.getListStudent();
				for (User user : list) {
					if (user.getEmail().equals(email)) {
						list.remove(user);

						classDAO.updateClass(myClass);
						break;
					}
				}
			}

			return true;
		} catch (Exception ex) {
			return false;
		}
	}

}
